package cuit.game.controller;

import cuit.game.entity.Application;
import cuit.game.entity.Contest;
import cuit.game.entity.Team;
import cuit.game.entity.User;
import cuit.game.service.ApplicationService;
import cuit.game.service.ContestService;
import cuit.game.service.TeamService;
import cuit.game.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/team")
public class TeamControler {

    //注入类
    @Autowired
    private TeamService teamService;

    @Autowired
    private UserService userService;

    @Autowired
    private ApplicationService applicationService;

    @Autowired
    private ContestService contestService;

    //创建队伍
    @GetMapping("/addteam")
    public String addTeam(@RequestParam("contestId") long contestId,Model model) {


        Contest contest = contestService.findById(contestId);

        model.addAttribute("contest", contest);
    	return "team/addteam"; }

    @PostMapping("/addteam2")
    public String add(@RequestParam("teamname") String name,
                      @RequestParam("content") String content,
                      @RequestParam("contestId") long contestId, Model model, HttpServletRequest request) {

    	System.out.print("发的换个地方");
         // 获取HttpSession对象
         HttpSession session = request.getSession();

         // 
         User user = (User)session.getAttribute("user");
         User user1 = userService.findById(user.getId());
         Contest contest = contestService.findById(contestId);

         Team team = new Team();
         Application application = new Application();

         team.setBody(team.getBody()+1);
         team.setUser(user1);
         team.setContest(contest);
         team.setContent(content);

         team.setTeamname(name);

         application.setTeam(team);
         application.setUser(user1);
         application.setContent(content);

         applicationService.save(application);
         teamService.save(team);
         model.addAttribute("teamlist", team);
         return "team/list";
    }

    //展示所有队伍
    @GetMapping("list")
    public String viewTeam(Model model) {

        List<Team> teams = teamService.findAll();
        model.addAttribute("teamlist", teams);
        return "team/list";
    }


    // 展示一个项目下的队伍
    @GetMapping("jointeam")
    public String viewContest(@RequestParam("contestId") long contestId, Model model) {

        List<Team> teams = teamService.findByContestId(contestId);
        model.addAttribute("teams", teams);
        return "team/jointeam";
    }

    //加入队伍
    @GetMapping("/jointeam2")
    public String jointeam(@RequestParam("teamId") long teamId, HttpServletRequest request,Model model) {

        //获取对应队伍
        Team team = teamService.findById(teamId);

        team.setBody(team.getBody() + 1);
        Application application = new Application();
        //获取session对象
        HttpSession session = request.getSession();

        //获取user
        User user = (User) session.getAttribute("user");
        User user1 = (User) userService.findById(user.getId());
        //初始化报名信息
        application.setTeam(team);
        application.setUser(user1);
        application.setContent(team.getContent());
        teamService.save(team);
        applicationService.save(application);
        List<Application> teams = applicationService.findByTeam_id(teamId);
		model.addAttribute("student", teams);
        return "user/contest";
    }

    //删除队伍
    @GetMapping("/delete")
    public String delete(@RequestParam("teamId") long teamId, Model model) {

        List<Application> application = applicationService.findByTeam_id(teamId);
        for (Application app : application) {
            applicationService.deleteById(app.getId());
        }
        teamService.deleteById(teamId);

        List<Team> teams = teamService.findAll();
        model.addAttribute("teamlist", teams);

        return "team/list";
    }

}
